package com.spring.blog.common;

import com.spring.blog.exception.BlogException;
import com.spring.blog.pojo.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.HandlerMethodValidationException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

@Slf4j
@ControllerAdvice
@ResponseBody
public class ExceptionAdvice {
    //统一异常处理
    @ExceptionHandler
    public Result exceptionHandler(Exception e){
        log.error("发生异常,e:",e);
        return Result.fail(e.getMessage());
    }
    @ExceptionHandler
    public Result exceptionHandler(BlogException e){
        log.error("发生异常，e:{}",e.getErrMsg());
        return Result.fail(e.getErrMsg());
    }
    @ExceptionHandler
    public Result exceptionHandler(MethodArgumentTypeMismatchException e){
        log.error("参数校验异常,出现null");
        return Result.fail("参数校验异常,出现null");
    }
    @ExceptionHandler
    public Result exceptionHandler(MethodArgumentNotValidException e){
        log.error("用户名或者密码都不能为空字符串");
        return Result.fail("用户名或者密码都不能为空字符串");
    }
    @ExceptionHandler
    public Result exceptionHandler(HandlerMethodValidationException e){
        log.error("参数校验失败");
        return Result.fail("参数校验失败");
    }

}
